package com.youlu.campus.admin.card.service;

import com.youlu.campus.admin.card.dto.VipGoodsDeleteDTO;
import com.youlu.campus.admin.card.dto.VipGoodsSaveDTO;
import com.youlu.campus.admin.card.dto.VipGoodsSearchDTO;
import com.youlu.campus.admin.card.dto.VipRecordSearchDTO;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.card.entity.VipRedeemCodeRecord;
import com.youlu.campus.entity.card.entity.VipGoods;
import com.youlu.campus.service.course.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author : [dengbo]
 * @className : ThirdGoodsService
 * @description : [vip商品服务]
 * @createTime : [2022/12/2 16:44]
 */
@Service
public class VipGoodsService {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 虚拟商品保存
     *
     * @param saveDTO
     * @return
     */
    public boolean save(VipGoodsSaveDTO saveDTO) {
        if(saveDTO.getProductId() == null){
            throw new BusinessException("商品id不能为空");
        }
        VipGoods goods = mongoTemplate.findOne(Query.query(Criteria.where("productId").is(saveDTO.getProductId())), VipGoods.class);
        if (goods == null) {
            goods = new VipGoods();
            goods.setCreatedTime(new Date());
            goods.setDeleted(false);
        }
        goods.setProductName(saveDTO.getProductName());
        goods.setProductId(saveDTO.getProductId());
        goods.setExpireAfterHour(12);
        goods.setExpireDay(saveDTO.getExpireDay());
        mongoTemplate.save(goods);
        return true;
    }

    /**
     * 虚拟商品列表
     *
     * @param dto
     * @return
     */
    public Page<VipGoods> list(VipGoodsSearchDTO dto) {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotBlank(dto.getProductName())) {
            query.like("productName",dto.getProductName());
        }
        query.addCriteria(Criteria.where("deleted").is(false));
        long count = mongoTemplate.count(query, VipGoods.class);
        query.with(Sort.by(Sort.Order.desc("createdTime")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<VipGoods> datas = mongoTemplate.find(query.with(pageRequest), VipGoods.class);
        return PageableExecutionUtils.getPage(datas, pageRequest, () -> count);
    }

    /**
     * 虚拟商品列表
     *
     * @param dto
     * @return
     */
    public Page<VipRedeemCodeRecord> recordList(VipRecordSearchDTO dto) {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotBlank(dto.getVirtualGoodsCode())) {
            query.like("virtualGoodsCode",dto.getVirtualGoodsCode());
        }
        if (StringUtils.isNotBlank(dto.getOrderNo())) {
            query.like("orderNo",dto.getOrderNo());
        }
        if (StringUtils.isNotBlank(dto.getSubOrderNo())) {
            query.like("subOrderNo",dto.getSubOrderNo());
        }
        if (StringUtils.isNotBlank(dto.getRedeeCode())) {
            query.like("redeeCode",dto.getRedeeCode());
        }
        long count = mongoTemplate.count(query, VipRedeemCodeRecord.class);
        query.with(Sort.by(Sort.Order.desc("createdTime")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<VipRedeemCodeRecord> datas = mongoTemplate.find(query.with(pageRequest), VipRedeemCodeRecord.class);
        return PageableExecutionUtils.getPage(datas, pageRequest, () -> count);
    }

    /**
     * 弹窗查询
     *
     * @return
     */
    public List<VipGoods> all() {
        QueryWrapper query = new QueryWrapper();
        query.addCriteria(Criteria.where("deleted").is(false));
        query.with(Sort.by(Sort.Order.desc("createdTime")));
        List<VipGoods> datas = mongoTemplate.find(query, VipGoods.class);
        return datas;
    }

    /**
     *  删除记录
     *
     * @param saveDTO
     * @return
     */
    public boolean delete(VipGoodsDeleteDTO saveDTO) {
        Query query = Query.query(Criteria.where("id").is(saveDTO.getId()));
        Update update = new Update();
        update.set("deleted", true);
        mongoTemplate.updateFirst(query, update, VipGoods.class);
        return true;
    }
}
